package com.ymatou.iapi.godmch.testcase;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.godmch.parameter.applySupplierBean;
import com.ymatou.iapi.godmch.parameter.createIndepMchBean;
import com.ymatou.iapi.godmch.parameter.deleteSupplierBean;
import com.ymatou.iapi.godmch.service.applyAgentCall;
import com.ymatou.iapi.godmch.service.applySupplierCall;
import com.ymatou.iapi.godmch.service.createIndepMchCall;
import com.ymatou.iapi.godmch.service.deleteSupplierCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.agent_type;
import com.ymttest.database.model.mch_info;
import com.ymttest.database.model.mch_relation;
import com.ymttest.database.model.godmch.supplierLog;
import com.ymttest.database.model.godmch.supplierLogExample;
import com.ymttest.database.sqlwapper.godmch.agent_typeWapper;
import com.ymttest.database.sqlwapper.godmch.mch_infoWapper;
import com.ymttest.database.sqlwapper.godmch.mch_relationWapper;
import com.ymttest.database.sqlwapper.godmch.supplierLogWapper;
/**
*删除供货商
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_deleteSupplier{
	private static deleteSupplierBean bean;
	private static deleteSupplierCall call;
	private static mch_infoWapper mchInfoWapper = new mch_infoWapper();
	private static mch_relationWapper relationWapper=new mch_relationWapper();
	private static 	agent_typeWapper agentTypeWapper=new agent_typeWapper();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("删除供货商");
	}

	@Before
	public void caseUp() {
		bean=new deleteSupplierBean();
		call=new deleteSupplierCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	/**
	 * 校验独立商户 和 供货商
	 * @param mchId
	 * @param supplierId
	 * @throws InterruptedException
	 */
	private void check_mchInfo(Integer mchId,Integer supplierId) throws InterruptedException{
		Logger.comment("1:验证数据库mch_info表");
		//独立商户
		Map<String, Object> map = new HashMap();
		map.put("mchId", mchId);
		mch_info  mchinfo=mchInfoWapper.selectby(map).get(0);
		//供货商
		Map<String, Object> supplier_map = new HashMap();
		supplier_map.put("mchId", supplierId);
		mch_info  supplier_mchinfo=mchInfoWapper.selectby(supplier_map).get(0);
		Logger.verifyEquals(1, mchinfo.getAgent(), "验证独立商户的Agent");
		Logger.verifyEquals(1, mchinfo.getChannels(), "验证独立商户的channels");
		Logger.verifyEquals(3, mchinfo.getMchShareType(), "验证独立商户的MchShareType");
		Logger.verifyEquals(0, mchinfo.getState(), "验证独立商户的State");
		Logger.verifyEquals(0, supplier_mchinfo.getAgent(), "验证供货商的Agent");
		Logger.verifyEquals(1, supplier_mchinfo.getChannels(), "验证供货商的channels");
		Logger.verifyEquals(5, supplier_mchinfo.getMchShareType(), "验证证供货商的MchShareType");
		Logger.verifyEquals(0, supplier_mchinfo.getState(), "验证证供货商的State=-2待审核");
	}
	/**
	 * 独立商户与供货商关系表
	 * @param mchId
	 * @param supplierId
	 * @throws InterruptedException
	 */
	private void check_relation(Integer mchId,Integer supplierId) throws InterruptedException
	{
		//独立商户与供货商关系表
		Logger.comment("2:验证数据库mch_relation表");
		//申请供货商关系时，独立商户是代理商，供货商是渠道商身份
		mch_relation relation=relationWapper.selectByPrimaryKey(mchId, supplierId).get(0);
		Logger.verifyEquals(-1, relation.getState(), "验证数据库mch_relation表中的state=-1");
		Logger.verifyEquals(1, relation.getAgentModel(), "验证数据库mch_relation表中的AgentModel=1");
		//查询渠道创建的代理类型
		Map<String, Object> map2 = new HashMap();
		map2.put("mchId", bean.getSupplierId());
		agent_type  type=agentTypeWapper.selectby(map2).get(0);
		Logger.verifyEquals(type.getAgentType(), relation.getAgentType(), "验证数据库mch_relation表中的AgentType");
	}
	/**
	 * 校验供货商日志
	 * @param mchId
	 * @param supplierId
	 * @param supplierLogNum
	 * @throws InterruptedException
	 */
	private void  check_supplierLog(Integer mchId,Integer supplierId) throws InterruptedException {
		//供货商日志
		Logger.comment("3:验证数据库supplierLog表");
		supplierLogExample supplierLogExample=new supplierLogExample();
		supplierLogWapper supplierLogWapper=new supplierLogWapper();
		supplierLogExample.createCriteria().andMchIdEqualTo(mchId).andSupplierIdEqualTo(supplierId).andLogTypeEqualTo(4);
		List<supplierLog> supplierLogs=  supplierLogWapper.selectByExample(supplierLogExample);
		Logger.verifyEquals(1, supplierLogs.size(), "验证supplierLog条数");
	}
	
	/**
	 * 校验
	 * @param mchId
	 * @throws JSONException
	 * @throws InterruptedException
	 */
	private void check_verify() throws JSONException, InterruptedException
	{
		//删除成功   1：商户表mch_info数据不变 2：关系表mch_relation数据清空
		Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
		Logger.verifyEquals(true, call.getMessage().contains("请求成功"), "验证Message");
		check_mchInfo(bean.getMchId(), bean.getSupplierId());
		check_relation(bean.getMchId(), bean.getSupplierId()) ;
		check_supplierLog(bean.getMchId(),bean.getSupplierId());
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_deleteSupplier_001() {
		Logger.start(true,"正例-删除供货商-独立商户id是无代理独立独立商户-传入正确的供货商-成功");
		try {
			//创建独立商户1
			createIndepMchBean createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
            createIndepMchCall createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
            //申请供货商
            applySupplierBean applySupplierBean=GodmchManageCallService.initApplySupplier(createIndepMchCall.getmchId());
            applySupplierCall applySupplierCall=GodmchManageCallService.applySupplierCall(applySupplierBean);
            //同意
            GodmchManageCallService.approveSupplier(createIndepMchCall.getmchId(), applySupplierCall.getSupplierId());
            
            bean.setMchId(applySupplierBean.getMchId());
            bean.setSupplierId(applySupplierCall.getSupplierId());
			call.setData(bean);
			call.callService();
			
			check_verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_deleteSupplier_002() {
		Logger.start(true,"正例-删除供货商-独立商户id是有代理独立商户-传入正确的供货商-成功");
		try {
			//创建独立商户1
			createIndepMchBean createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
            createIndepMchCall createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
            //申请代理
            GodmchManageCallService.applyAndapproveAgent(createIndepMchCall.getmchId());
            //申请供货商
            applySupplierBean applySupplierBean=GodmchManageCallService.initApplySupplier(createIndepMchCall.getmchId());
            applySupplierCall applySupplierCall=GodmchManageCallService.applySupplierCall(applySupplierBean);
            //同意
            GodmchManageCallService.approveSupplier(createIndepMchCall.getmchId(), applySupplierCall.getSupplierId());
            
            bean.setMchId(applySupplierBean.getMchId());
            bean.setSupplierId(applySupplierCall.getSupplierId());
			call.setData(bean);
			call.callService();
			
			check_verify();		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
//	@Test
//	@Category({P1.class})
//	@TestCase
//	public void Tc_deleteSupplier_003() {
//		Logger.start(true,"正例-删除供货商-独立商户id是独立商户的供货商-传入正确的供货商-成功");
//		try {
//			//TODO 补全case代码
//			org.junit.Assert.fail();
//			call.setData(bean);
//			call.callService();			
//			
//		} catch (Exception e) {
//			Logger.fail(e);
//		}
//	}
//	@Test
//	@Category({P1.class})
//	@TestCase
//	public void Tc_deleteSupplier_004() {
//		Logger.start(true,"正例-删除供货商-独立商户id是独立独立商户的代理-传入正确的供货商-成功");
//		try {
//			//TODO 补全case代码
//			org.junit.Assert.fail();
//			call.setData(bean);
//			call.callService();			
//			
//		} catch (Exception e) {
//			Logger.fail(e);
//		}
//	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_deleteSupplier_005() {
		Logger.start(true,"正例-删除供货商-独立商户id是独立独立商户的代理-传入的是代理-失败");
		try {
			//创建独立商户1
			createIndepMchBean createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
            createIndepMchCall createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
            //申请代理
            applyAgentCall applyAgentCall=GodmchManageCallService.applyAndapproveAgent(createIndepMchCall.getmchId());
            //申请供货商
            applySupplierBean applySupplierBean=GodmchManageCallService.initApplySupplier(createIndepMchCall.getmchId());
            applySupplierCall applySupplierCall=GodmchManageCallService.applySupplierCall(applySupplierBean);
            //同意
            GodmchManageCallService.approveSupplier(createIndepMchCall.getmchId(), applySupplierCall.getSupplierId());
            
            bean.setMchId(applyAgentCall.getAgentId());
            bean.setSupplierId(applySupplierCall.getSupplierId());
			call.setData(bean);
			call.callService();
			
			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("供货关系不存在"), "验证Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_deleteSupplier_006() {
		Logger.start(true,"正例-删除供货商-独立商户与供货商不存在关系-失败");
		try {
			//创建独立商户1
			createIndepMchBean createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
            createIndepMchCall createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
            
			//创建独立商户2
			createIndepMchBean createIndepMchBean2=GodmchManageCallService.InitCreateIndepMchBean();
            createIndepMchCall createIndepMchCall2=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean2);
            
            //申请代理
            applyAgentCall applyAgentCall=GodmchManageCallService.applyAndapproveAgent(createIndepMchCall.getmchId());
            //申请供货商
            applySupplierBean applySupplierBean=GodmchManageCallService.initApplySupplier(createIndepMchCall.getmchId());
            applySupplierCall applySupplierCall=GodmchManageCallService.applySupplierCall(applySupplierBean);
            //同意
            GodmchManageCallService.approveSupplier(createIndepMchCall.getmchId(), applySupplierCall.getSupplierId());
            
            bean.setMchId(createIndepMchCall2.getmchId());
            bean.setSupplierId(applySupplierCall.getSupplierId());
			call.setData(bean);
			call.callService();
			
			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("供货关系不存在"), "验证Message");			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_deleteSupplier_007() {
		Logger.start(true,"正例-删除供货商-mchId不存在-失败");
		try {
			//创建独立商户1
			createIndepMchBean createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
            createIndepMchCall createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
            //申请代理
            applyAgentCall applyAgentCall=GodmchManageCallService.applyAndapproveAgent(createIndepMchCall.getmchId());
            //申请供货商
            applySupplierBean applySupplierBean=GodmchManageCallService.initApplySupplier(createIndepMchCall.getmchId());
            applySupplierCall applySupplierCall=GodmchManageCallService.applySupplierCall(applySupplierBean);
            //同意
            GodmchManageCallService.approveSupplier(createIndepMchCall.getmchId(), applySupplierCall.getSupplierId());
            
            bean.setMchId(createIndepMchCall.getmchId()+9999);
            bean.setSupplierId(applySupplierCall.getSupplierId());
			call.setData(bean);
			call.callService();
			
			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("供货关系不存在"), "验证Message");			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_deleteSupplier_008() {
		Logger.start(true,"正例-删除供货商-supplierId不存在-失败");
		try {
			//创建独立商户1
			createIndepMchBean createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
            createIndepMchCall createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
            //申请代理
            applyAgentCall applyAgentCall=GodmchManageCallService.applyAndapproveAgent(createIndepMchCall.getmchId());
            //申请供货商
            applySupplierBean applySupplierBean=GodmchManageCallService.initApplySupplier(createIndepMchCall.getmchId());
            applySupplierCall applySupplierCall=GodmchManageCallService.applySupplierCall(applySupplierBean);
            //同意
            GodmchManageCallService.approveSupplier(createIndepMchCall.getmchId(), applySupplierCall.getSupplierId());
            
            bean.setMchId(createIndepMchCall.getmchId());
            bean.setSupplierId(applySupplierCall.getSupplierId()+9999);
			call.setData(bean);
			call.callService();
			
			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("供货关系不存在"), "验证Message");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}